const { userModel, PatientModel } = require("../model");
//添加就诊人
exports.addPatient = async (req,res)=>{
    const { name,idType,idNo,sex,birthday,maritalStatus,insuType,currentAddress,detailAddress,emergencyContactName,emergencyContactPhone,emergencyContactIdType,emergencyContactIdNo } = req.body
    let userId = req.user?._id
    if(idNo==emergencyContactIdNo) {
        return res.status(400).json({
            code:400,
            message:'就诊人身份证号不能与紧急联系人身份证号相同',
            data:null
        })
    }
    //判断添加就诊人人数是否上限
    let count = await PatientModel.find({user:userId})
    if(count.length>6) {
        return res.status(400).json({
            code:400,
            message:'添加就诊人人数上限',
            data:null
            })
    }
    //判断信息是否完善
    if(!name || !idType || !idNo || !sex || !birthday || !maritalStatus || !insuType || !currentAddress || !detailAddress || !emergencyContactName || !emergencyContactPhone || !emergencyContactIdType || !emergencyContactIdNo){
        return res.status(400).json({
            code:400,
            message:'信息不完整',
            data:null
        })
    }
    //判断是否实名认证
    let result = await userModel.findById(userId)
    if(result.realName!==2) {
        return res.status(400).json({
            code:400,
            message:'请先实名认证',
            data:null
        })
    }
    //判断是否添加重复就诊人
    let result2 = await PatientModel.findOne({idNo,idType})
    if(result2){
        return res.status(400).json({
            code:400,
            message:'就诊人已存在',
            data:null
        })
    }
    
    //添加就诊人
    try {
        let patient = new PatientModel({
            name,
            user:userId,
            idType,
            idNo,
            sex,
            birthday,
            maritalStatus,
            insuType,
            currentAddress,
            detailAddress,
            emergencyContactName,
            emergencyContactPhone,
            emergencyContactIdType,
            emergencyContactIdNo,
        })
        await patient.save()
        res.status(200).json({
            code:200,
            message:'添加就诊人成功',
            data:null
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'添加就诊人失败',
            data:error
        })
    }
}
//获取用户就诊人列表
exports.getPatientList = async (req,res)=>{
    let userId = req.user?._id
    try {
        let result = await PatientModel.find({user:userId})
        res.status(200).json({
            code:200,
            message:'获取就诊人列表成功',
            data:result
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'获取就诊人列表失败',
            data:error
        })
    }
}
//修改就诊人信息
exports.updatePatient = async (req,res)=>{
    let { id } = req.query
    if(!id) {
        return res.status(400).json({
            code:400,
            message:'参数错误',
            data:null
        })
    }

    // let isCf = await PatientModel.findOne({idNo:req.body?.idNo})
    // if(isCf) {
    //     return res.status(400).json({
    //         code:400,
    //         message:'就诊人已存在',
    //         data:null
    //     })
    // }
    try {
        let result = await PatientModel.findByIdAndUpdate(id,req.body,{new:true})
        res.status(200).json({
            code:200,
            message:'修改就诊人信息成功',
            data:result
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'修改就诊人信息失败',
            data:error
        })
    }
}
//删除就诊人接口
exports.deletePatient = async (req,res)=>{
    const { id } = req.query
    if(!id) {
        return res.status(400).json({
            code:400,
            message:'参数错误',
            data:null
        })
    }
    try {
        let result = await PatientModel.findByIdAndDelete(id)
        res.status(200).json({
            code:200,
            message:'删除就诊人成功',
            data:result
        })
    }catch (error) {
        res.status(500).json({
            code:500,
            message:'删除就诊人失败',
            data:error
        })
    }
}
//获取就诊人详情
exports.getPatientDetail = async (req,res)=>{
    const { id } = req.query
    if(!id) {
        return res.status(400).json({
            code:400,
            message:'参数错误',
            data:null
        })
    }
    try {
        let result = await PatientModel.findById(id)
        res.status(200).json({
            code:200,
            message:'获取就诊人详情成功',
            data:result
        })
    } catch (error) {
        res.status(500).json({
            code:500,
            message:'获取就诊人详情失败',
            data:error
        })
    }
}